﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;

namespace StudentManage.Web.Post
{
    public partial class PostInfo : SecurityPage
    {
        private void LoadData()
        {
            int Pid = Convert.ToInt32(Request.QueryString["Pid"].ToString());
            Project md = Project.Where("id=@0", Pid).First();
            this.lab_AddTtime.Text = Convert.ToDateTime(md.AddTime).ToString("yyyy-MM-dd");
            this.lab_PositionType.Text = TypeInfo.Where("id=@0", int.Parse(md.PositionTypeId.ToString())).First().TypeName;
            this.lab_RecruitCount.Text = md.RecruitCount.ToString();
            this.lab_Title.Text = md.Title.ToString();
            this.lab_Contents.Text = md.Contents;
            xy_bmxbm md_bm = xy_bmxbm.Where("BMDM=@0", md.Departmentid).FirstOrDefault();
            if (md_bm != null)
            {
                this.lab_DeptName.Text = md_bm.BMMC;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            if (Request.QueryString["Pid"] != null)
                this.LoadData();
        }

        protected void Btn_Submint_Click(object sender, EventArgs e)
        {
            if (Session["UserMD"] == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>$(document).ready(function () {alert('请先登录！');});</script>");
                return;
            }

            if (Apply.Where("Userid=@0 and IsDel=@1 and Projectid=@2", ((UserList)Session["UserMD"]).id, false, Convert.ToInt32(Request.QueryString["Pid"].ToString())).Count() > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>$(document).ready(function () {alert('不能重复申请！');});</script>");
                return;
            }

            if (Apply.Where("Userid=@0 and IsDel=@1 and IsAudit=@2", ((UserList)Session["UserMD"]).id, false, true).Count() > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>$(document).ready(function () {alert('对不起！您已经存在已经审批通过的岗位！');});</script>");
                return;
            }

            Project md = Project.Where("id=@0", Convert.ToInt32(Request.QueryString["Pid"].ToString())).First();
            UserList user = Session["UserMD"] as UserList;
            xy_xsxx xsxx = xy_xsxx.Where("XH=@0",user.XH).FirstOrDefault();
            xydm xy = xydm.Where("XYDM=@0", md.College).FirstOrDefault();

            //判断 是否有关联的学生信息
            if (xsxx == null) 
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>$(document).ready(function () {alert('数据信息错误,或非学生！');});</script>");
                return;
            }

            //判断招募范围是否包括该学生所在学院
            xy_bmxbm xy_dm=xy_bmxbm.Where("BMMC=@0",xsxx.XY).FirstOrDefault();
            string strRecruitRange="|"+md.RecruitRange+"|";
            if (md.College != "0" && xy_dm != null && strRecruitRange.IndexOf("|" + xy_dm.BMDM + "|") < 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>$(document).ready(function () {alert('对不起，您不能申请该岗位或活动！');});</script>");
                return;
            }
            //判断隶属学院是否为本学院
            //if (md.College != "0" && xy != null && xy.XYMC != xsxx.XY)
            //{
            //    ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>$(document).ready(function () {alert('不能申请其他学院！');});</script>");
            //    return;
            //}

            ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>$(document).ready(function () {Showdlg();});</script>");
        }

        protected void btPost_Click(object sender, EventArgs e)
        {
            Apply md = new Apply();
            md.Projectid = Convert.ToInt32(Request.QueryString["Pid"].ToString());
            md.ProjectTitle = lab_Title.Text.Trim();
            md.Userid = ((UserList)Session["UserMD"]).id;
            md.UserName = ((UserList)Session["UserMD"]).UName;
            md.Reason = this.txt_Reason.Text.Trim();
            md.ApplyTime = DateTime.Now;
            md.IsAudit = false;
            md.IsEvaluate = false;
            md.IsDel = false;
            md.AddTime = DateTime.Now;
            Apply.Insert(md);
            ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>$(document).ready(function () {alert('申请成功！');});</script>");
        }
    }
}